const mongoose = require('mongoose');
const Movie = mongoose.model('Movie');

/**
 * 查询所有电影数据
 * @param {电影类型} type 
 * @param {电影时间} year 
 */
export const getAllMovies = async (type, year) => {
    let query = {};
    if (type) {
        query.movieTypes = {
            $in: [type]
        }
    }
    if (year) {
        query.year = year
    }
    const movies = await Movie.find(query);
    return movies;
}

/**
 * 根据id查询单条电影数据
 * @param {doubanId} id 
 */
export const getMovieDetail = async (id) => {
    const movie = await Movie.findOne({ _id: id });
    return movie;
}

/**
 * 查找相关电影数据
 * @param {要查找的类似电影} movie 
 */
export const getRelativeMovies = async (movie) => {
    const movies = await Movie.find({
        movieTypes: { $in: movie.movieTypes }
    })
    return movies;
}